package com.eke.action.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import com.eke.bean.Users;
import com.eke.service.UsersService;
import com.jason.mvc.annotations.Ajax;
import com.jason.mvc.annotations.Namespace;
import com.jason.mvc.annotations.RequestMapping;
import com.jason.mvc.utils.ApplicationContext;

@Namespace("/users")
@Component("UsersAction")
@Scope(WebApplicationContext.SCOPE_REQUEST)
public class UsersAction {

	@Resource
	private UsersService usersService;
	private Users users;
	private Integer pageCount = 1;
	private Integer pageSize = 10;


	/**
	 * 增加用户
	 * 
	 * @return map
	 */
	@RequestMapping("add")
	@Ajax(true)
	public Map<String, Object> addUsers() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", usersService.addUser(users));
		return map;
	} 

	/**
	 * 验证用户登录
	 * 
	 * @return Boolean
	 */
	@RequestMapping("verUser")
	@Ajax(true)
	public Map<String, Object> verUser() {
		javax.servlet.http.HttpSession session = ApplicationContext.getSession();
		session.setAttribute("userName", users.getUserName());
		Map<String, Object> map = new HashMap<String, Object>();
		boolean b = usersService.verUser(users);
		map.put("result", b);
		return map;
	}

	/**
	 * 验证用户名是否唯一
	 * 
	 * @return
	 */
	@RequestMapping("verUserName")
	@Ajax(true)
	public Map<String, Object> varUserName() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", usersService.isExitUserName(users));
		return map;
	}

	/**
	 * 根据用户id查询用户信息
	 * 
	 * @return
	 */
	@RequestMapping("queryUserDetails")
	@Ajax(true)
	public Map<String, Object> queryUserDetails() {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map.put("result", usersService.queryUserDetails(users.getUserId()));
		} catch (EmptyResultDataAccessException e) {
			map.put("result", "null");
		}
		return map;
	}

	/**
	 * 根据所有用户
	 * @return
	 */
	@RequestMapping("queryAllUsers")
	@Ajax(true)
	public Map<String, Object> queryAllUsers() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "success");
		map.put("count", usersService.queryAllUsers((getPageCount()-1)*getPageSize(), getPageSize()).size());
		map.put("data", usersService.queryAllUsers((getPageCount()-1)*getPageSize(), getPageSize()));
		return map;
	}
	
	public Integer getPageCount() {
		return pageCount < 1 ? 1 : pageCount;
	}

	public Integer getPageSize() {
		return pageSize < 0 ? 10 : pageSize;
	}
	
	/**
	 * 根据用户名查询用户结合分页
	 * @return
	 */
	@RequestMapping("queryUsers")
	@Ajax(true)
	public Map<String, Object> queryUsers() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Users> list = usersService.queryUsers(users.getUserName(), (getPageCount()-1)*getPageSize(), getPageSize());
		map.put("code", 0);
		map.put("msg", "success");
		map.put("count", list.size());
		map.put("data", list);
		return map;
	}
	/**
	 * 分页查询所有用户
	 * @return
	 */
	@RequestMapping("queryUsersFy")
	@Ajax(true)
	public Map<String, Object> queryUsersFy() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Users> list = usersService.queryAllUsers((getPageCount()-1)*getPageSize(), getPageSize());
		map.put("code", 0);
		map.put("msg", "success");
		map.put("count", list.size());
		map.put("data", list);
		return map;
	}
}
